{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup Box Client\n",
    "from box_sdk_gen import DeveloperTokenConfig, BoxDeveloperTokenAuth, BoxClient\n",
    "\n",
    "BOX_DEV_TOKEN = \"your_box_dev_token\"\n",
    "\n",
    "config = DeveloperTokenConfig(BOX_DEV_TOKEN)\n",
    "auth = BoxDeveloperTokenAuth(config)\n",
    "box_client = BoxClient(auth)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"your-key\"\n",
    "\n",
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "from llama_index.llms.openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.tools.box import (\n",
    "    BoxSearchByMetadataToolSpec,\n",
    "    BoxSearchByMetadataOptions,\n",
    ")\n",
    "\n",
    "# Define your metadata search query\n",
    "# This must be done when instantiating the tool because the AI agent wont have a clue how to execute the search\n",
    "\n",
    "# Parameters\n",
    "from_ = \"enterprise_\" + \"your_box_enterprise_id\" + \".\" + \"your_metadata_template_key\"\n",
    "ancestor_folder_id = \"your_starting_folder_id\"\n",
    "query = \"documentType = :docType \"  # Your metadata query string\n",
    "query_params = '{\"docType\": \"Invoice\"}'  # Your metadata query parameters\n",
    "\n",
    "# Search options\n",
    "options = BoxSearchByMetadataOptions(\n",
    "    from_=from_,\n",
    "    ancestor_folder_id=ancestor_folder_id,\n",
    "    query=query,\n",
    ")\n",
    "\n",
    "box_tool = BoxSearchByMetadataToolSpec(box_client=box_client, options=options)\n",
    "\n",
    "agent = FunctionAgent(\n",
    "    tools=box_tool.to_tool_list(),\n",
    "    llm=OpenAI(model=\"gpt-4.1\"),\n",
    ")\n",
    "\n",
    "answer = await agent.run(\n",
    "    f\"search all documents using the query_params as the key value pair of  {query_params} \"\n",
    ")\n",
    "print(answer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "tests/test_tools_box_search_by_metadata.py Added user message to memory: search all documents using the query_params as the key value pair of  {\"docType\": \"Invoice\"} \n",
    "=== Calling Function ===\n",
    "Calling function: search with args: {\"query_params\":\"{\\\"docType\\\": \\\"Invoice\\\"}\"}\n",
    "========================\n",
    "I found the following documents with the query parameter \"docType: Invoice\":\n",
    "\n",
    "1. Document ID: ee053400-e501-49d0-9e9f-e021bd86d9c6\n",
    "   - Name: Invoice-B1234.txt\n",
    "   - Size: 168 bytes\n",
    "   - Created By: RB Admin\n",
    "   - Created At: 2024-04-30 06:22:15\n",
    "\n",
    "2. Document ID: 475cfee1-557a-4a3b-bc7c-18634d3a5d99\n",
    "   - Name: Invoice-C9876.txt\n",
    "   - Size: 189 bytes\n",
    "   - Created By: RB Admin\n",
    "   - Created At: 2024-04-30 06:22:17\n",
    "\n",
    "3. Document ID: 9dbfd4ec-639f-4c7e-8045-645866e780a3\n",
    "   - Name: Invoice-Q2468.txt\n",
    "   - Size: 176 bytes\n",
    "   - Created By: RB Admin\n",
    "   - Created At: 2024-04-30 06:22:19\n",
    "\n",
    "These are the documents matching the query parameter. Let me know if you need more information or assistance.\n",
    "\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama-index-readers-box-9HYYEbiN-py3.12",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
